package org.nepu.su.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.core.auth.anno.AuthLogin;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.jpa.utils.JpaDataUtil;
import org.nepu.sc.mx.SafeMx;
import org.nepu.sc.mx.SafeMxService;
import org.nepu.sc.mx.params.SafeMxParams;
import org.nepu.sc.mx.params.SafeMxQueryParams;
import org.nepu.sc.task.SafeTask;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("safe")
@Api(tags = "评价矩阵管理")
@AllArgsConstructor
public class SafeMxController {
    SafeMxService mxService;


    @AuthLogin
    @PostMapping("mx")
    @ApiOperation(value = "创建/更新评价矩阵")
    public ResponseDTO<SafeMx> setMx(@Validated @RequestBody SafeMxParams params) {
        // 根据 rateId 查询
        List<SafeMx> mxs = mxService.getByRateId(params.getRateId());
        if (mxs.isEmpty()){
            SafeMx mx = new SafeMx();
            BeanUtil.copyProperties(params, mx, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
            return ResponseDTO.succData(mxService.saveOne(mx));
        }else {
            SafeMx mx = mxs.get(0);
            mx.setMx(params.getMx());
            return ResponseDTO.succData(mxService.updateOne(mx));
        }
    }


    @AuthLogin
    @DeleteMapping("mx/{rateId}")
    @ApiOperation(value = "删除评价矩阵(根据评价ID)")
    public ResponseDTO<Object> deleteMx(@PathVariable("rateId") String rateId) {
        List<SafeMx> mxs = mxService.getByRateId(rateId);
        List<String> batchDeleteIds = mxs.stream().map(SafeMx::getId).collect(Collectors.toList());
        mxService.batchDelete(batchDeleteIds);
        return ResponseDTO.succMsg("删除成功");
    }


    @AuthLogin
    @GetMapping("mx/{rateId}")
    @ApiOperation(value = "获取评价矩阵详情(根据评价ID)")
    public ResponseDTO<Object> getMx(@PathVariable("rateId") String rateId) {
        List<SafeMx> mxs = mxService.getByRateId(rateId);
        if (mxs.isEmpty()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND,"未找到");
        }else {
            return ResponseDTO.succData(
                    mxs.get(0)
            );
        }
    }


    @AuthLogin
    @GetMapping("mx")
    @ApiOperation(value = "获取评价矩阵列表")
    public ResponseDTO<Object> getMxList(SafeMxQueryParams params){
        Pageable pageable = JpaDataUtil.buildPageable(params);
        Specification<SafeMx> spec = (root, query, cb) -> {
            return cb.like(root.get("rateId"),"%"+params.getSearchKey()+"%");
        };
        if (Validator.isEmpty(params.getSearchKey())){
            return ResponseDTO.succData(
                    mxService.getAll(pageable)
            );
        }else {
            return ResponseDTO.succData(
                    mxService.getAll(pageable,spec)
            );
        }
    }

}
